//
//  BaseSectionCollectionView.m
//  智云网校
//
//  Created by 毕武侠 on 2017/5/19.
//  Copyright © 2017年 bwx. All rights reserved.
//

#import "BaseSectionCollectionView.h"
#import "BaseCollectionReusableView.h"
#import "BaseCollectionViewCell.h"

@interface BaseSectionCollectionView () <cellDidActionDelegate>

@property (nonatomic, strong) NSString *headIdentifierID;
@property (nonatomic, strong) NSString *footIdentifierID;

@end

@implementation BaseSectionCollectionView

- (void)registerClass:(nullable Class)viewClass forSupplementaryViewOfKind:(NSString *)elementKind withReuseIdentifier:(NSString *)identifier {
    if ([elementKind isEqualToString:UICollectionElementKindSectionHeader]) {
        self.headIdentifierID = identifier;
    } else if ([elementKind isEqualToString:UICollectionElementKindSectionFooter]) {
        self.footIdentifierID = identifier;
    }
    [super registerClass:viewClass forSupplementaryViewOfKind:elementKind withReuseIdentifier:identifier];
}

- (void)registerNib:(nullable UINib *)nib forSupplementaryViewOfKind:(NSString *)kind withReuseIdentifier:(NSString *)identifier {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        self.headIdentifierID = identifier;
    } else if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
        self.footIdentifierID = identifier;
    }
    [super registerNib:nib forSupplementaryViewOfKind:kind withReuseIdentifier:identifier];
}

#pragma mark - collection headView
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    BaseSectionCollectionModel *model = self.sourceData[section];
    return  model.cellSize;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        BaseSectionCollectionModel *model = self.sourceData[indexPath.section];
        BaseCollectionReusableView *headView = (BaseCollectionReusableView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:self.headIdentifierID forIndexPath:indexPath];
        headView.indexPath = indexPath;
        [headView bindModel:model];
        return headView;
    }
    return nil;
}

#pragma mark  collectionView delegate dataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.sourceData.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    BaseSectionCollectionModel *model = self.sourceData[section];
    return model.data.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    BaseSectionCollectionModel *model = self.sourceData[indexPath.section];
    BaseCollectionModel *cellModel = model.data[indexPath.row];
    return  cellModel.cellSize;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BaseSectionCollectionModel *model = self.sourceData[indexPath.section];
    BaseCollectionModel *cellModel = model.data[indexPath.row];
    
    BaseCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:
                                    self.identifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[BaseCollectionViewCell alloc] init];
    }
    
    cell.count          = model.data.count;
    cell.indexPath      = indexPath;
    cell.actionDelegate = self;
    [cell bindModel:cellModel];
    return cell;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
